package org.sgs.reddit.bot.json;

import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.map.ObjectMapper;


public class RedditJsonConverter {

  private static final ObjectMapper mapper = new ObjectMapper();


  public static RedditLogin getRedditLogin(String rawJson) {
    return (RedditLogin) getRedditObject(IOUtils.toInputStream(rawJson), RedditLogin.class);
  }


  public static RedditLogin getRedditLogin(InputStream jsonStream) {
    return (RedditLogin) getRedditObject(jsonStream, RedditLogin.class);
  }


  public static RedditMe getRedditMe(String rawJson) {
    return (RedditMe) getRedditObject(IOUtils.toInputStream(rawJson), RedditMe.class);
  }


  public static RedditMe getRedditMe(InputStream jsonStream) {
    return (RedditMe) getRedditObject(jsonStream, RedditMe.class);
  }


  public static RedditListing getRedditListing(String rawJson) {
    return (RedditListing) getRedditObject(IOUtils.toInputStream(rawJson), RedditListing.class);
  }


  public static RedditListing getRedditListing(InputStream jsonStream) {
    return (RedditListing) getRedditObject(jsonStream, RedditMe.class);
  }


  private static Object getRedditObject(InputStream jsonStream, Class<?> clazz) {
    try {
      return mapper.readValue(jsonStream, clazz);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

}
